package com.algorithm4;

import java.util.Arrays;

public class QuickSort {

    public static void quickSort(int[] array,int p,int r){
        if(p>=r){
            return;
        }
        int pivot = partion(array,p,r);
        quickSort(array,p,pivot-1);
        quickSort(array,pivot+1,r);
    }

    public static int partion(int[] array,int p,int r){
        int pivot = r;
        int i = p;
        for(int j=i;j<r;j++){
            if(array[j]<array[pivot]){
                swap(array,j,i);
                i++;
            }
        }
        swap(array,i,pivot);
        return i;
    }

    public static void swap(int[] array,int i,int j){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args){
      int[] array = new int[]{5,7,6,8,9,3,2,4,1,10};
      System.out.println("排序前:"+ Arrays.toString(array));
      quickSort(array,0,array.length-1);
      System.out.println("排序后:"+ Arrays.toString(array));

    }
}
